import { UserLoginAPI } from '@/api/user'
import { resetRouter } from '@/router'

// import { getUserInfoAPI, getUserDetailByIdAPI } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: '',
    userInfo: '',
    ihrmTime: 0
  },
  mutations: {
    // 存储用户token
    setUserToken(state, token) {
      state.token = token
    },
    // 清除用户token
    removeUserToken(state) {
      state.token = ''
    },
    // 存储用户信息
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    // 清除用户信息
    removeUserInfo(state) {
      state.userInfo = ''
    },
    // 登录时存入时间戳
    setIhrmTime(state, time) {
      state.ihrmTime = time
    }
  },
  actions: {
    // 用户登录 获取用户token
    async userLogin({ commit }, plylod) {
      const data = await UserLoginAPI(plylod)
      commit('setUserToken', data.token)
      commit('setUserInfo', data)
      // window.localStorage.setItem('userData', JSON.stringify(data))
      // console.log(data)
      // commit('setIhrmTime', Date.now())
    },
    // 获取用户信息
    // async getUserInfo({ commit }) {
    //   const data = await getUserInfoAPI()
    //   const detail = await getUserDetailByIdAPI(data.userId)
    //   commit('setUserInfo', { ...data, ...detail })
    // },
    // 用户退出
    logout({ commit }) {
      commit('removeUserToken')
      commit('removeUserInfo')
      resetRouter()
    }
  }
}
